package bme.data {

	/**
	 * @author Rendall
	 */
	public class BRC_DataType {
		import bme.data.datatypes.*;

		/**
		 * BRC_DataType Constructor
		 */
		public function BRC_DataType(dataObj : Object = null) {
			if (dataObj) compose(dataObj);
		}

		public function compose(dataObj : Object) : void {
			for (var property : String in dataObj) setProperty(property, dataObj[property]);
		}

		protected function setProperty(property : String, value : *) : void {
			switch (property) {
				case "year":
					this[property] = new BRC_Year(value);
					break;                    
				case "street_line":
				case "location_point":
				case "location_line":				case "location_poly":
				case "location_track":
				case "location_multigeom":
					this[property] = new BRC_Location(value);
					break;
				case "hosted_by_camp": 
					this[property] = BRC_Camp.getCamp(value); 
					break;
				case "located_at_art" : 
					this[property] = BRC_Art.getArt(value);
					break;
				case "circular_street": 
					this[property] = new BRC_CStreet(value);
					break;
				default:
					this[property] = value;
			}
		}

		public static function getMainClass(dataObj : Object) : Class {
			// simplest to more complex
			var classes : Array = [BRC_Camp,BRC_Art,BRC_Event,BRC_Infrastructure];
			
			var brcClass : Class;
			var isClass : Boolean;
			
			for each (brcClass in classes) {
				var testInstance : * = new brcClass();
				isClass = true;
				
				for (var prop : String in dataObj) {
					if (!testInstance.hasOwnProperty(prop)) {
						isClass = false;
						break;			        	
					}
				}
			    
				if (isClass) {
					return brcClass;
					break;	
				}
			}
			
			//throw new Error("Unknown object " + dataObj.toString());
			return null;
		}
	}
}
